/* eslint-disable */
import axios from 'axios';
import { Modal } from 'ant-design-vue'
// import { Loading } from 'element-ui';
// import { baseUrl } from '@/config';
let baseUrl = 'api'; // 开发环境
// let baseUrl = 'signupServer'; // 测试环境
// let baseUrl = 'chinaPressServer';  // 正式环境
// import { getUUID, shaNum, setCookie, getCookie, storage } from '_@/js/utils';
// import { getUUID, shaNum, setCookie, getCookie } from '_@/js/utils';
axios.defaults.timeout = 300000;
// axios.defaults.headers.common['authorization'] =  sessionStorage.getItem('authorization')
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8,';
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';

const baseURL = `${window.location.origin}/${baseUrl}`;
axios.defaults.baseURL = baseURL;

// let loading = null;
// let count = 0;

// const countDown = () => {
// 	count = count > 0 ? --count : count;
// 	if (count < 1) {
// 		if (loading) {
// 			loading.close();
// 			loading = null;
// 		}
// 	}
// };

// 格式化参数
const formatParam = (params) => {
  let p = {}
  for (let key in params) {
    if (params[key] != undefined && params[key] != 'undefined') {
      p[`${key}`] = params[key];
    }
  }
  return p;
};

// post请求序列化参数
axios.interceptors.request.use(config => {
  // Object.defineProperties(Content-Type, 'application/json')
  // let isLoading = (config.data && config.data.isLoading) || false;
  // if (!loading && !isLoading) {
  // 	loading = Loading.service({
  // 		lock: false,
  // 		text: '拼命加载中...',
  // 		background: 'rgba(0, 0, 0, .7)'
  // 	});
  // }
  // count++;
  // 序列化post请求参数 post请求参数一律被转form格式数据
  if (config.method.toLocaleLowerCase() === 'post') {
    if (config.url != 'upload' && config.url != 'import/institutionsStudents') {
      let fd = new FormData();
      let data = formatParam(config.data);
      for (var key in data) {
        fd.append(key, data[key]);
      }
      config.data = fd;
    }
  }

  // get请求添加时间戳，防止IE下缓存
  if (config.method.toLocaleLowerCase() === 'get') {
    config.params = formatParam(config.params);
    config.params._t = new Date().getTime();
  }
  // if (getCookie('sessionId')) {
  // 	config.headers['sessionId'] = getCookie('sessionId');
  // } else {
  // 	config.headers['sessionId'] = shaNum(getUUID(10));
  // 	setCookie('sessionId', config.headers['sessionId']);
  // }
  return config;
}, error => {
  return Promise.reject(error);
});

// 返回数据处理
axios.interceptors.response.use(response => {
  // 等到所有接口都请求完成再去关闭loading
  // countDown();
  // Modal.destroyAll()
  // Modal.error({
  //   title: '123',
  //   onOk() {
  //     console.log('点击ok')
  //   }
  // })
  return response.data;
}, error => {
  let text = null
  if (/451/.test) text = '请登录账号'
  if (/452/.test) text = '登录失效，请重新登录'
  if (/453/.test) text = '该账号在其他地方登录'
  // Modal.destroyAll()
  // Modal.error({
  //   title: text,
  //   onOk(){
  //     window.location.href="/login "
  //   }
  // })
  return Promise.reject(text != null ? text : error)
})

const ajax = (options) => {
  options.method = options.method || 'get';
  if (options.method.toLocaleLowerCase() === 'get') {
    // get请求
    return new Promise((resolve, reject) => {
      axios.get(options.url, {
        params: options.params || {}
      }).then(response => {
        if (response.state && (response.state === '405' || response.state === '406')) {
          // location.href = '/' + baseUrl;
          // let LoginAddres = storage.get('LoginAddres'); // 退出地址
          // storage.clear(); // 清除缓存
          // window.location.href = LoginAddres + 'link/';
        } else {
          resolve(response);
        }
      }).catch(error => {
        // 等到所有接口都请求完成再去关闭loading
        // countDown();
        reject(error);
      });
    });
  } else {
    // post请求
    return new Promise((resolve, reject) => {
      axios
        .post(options.url, options.params || {})
        .then(response => {
          if (response.state && (response.state === '405' || response.state === '406')) {
            // location.href = '/' + baseUrl;
            // let LoginAddres = storage.get('LoginAddres'); // 退出地址
            // storage.clear(); // 清除缓存
            // window.location.href = LoginAddres + 'link/';
          } else {
            resolve(response);
          }
        })
        .catch(error => {
          // 等到所有接口都请求完成再去关闭loading
          // countDown();
          reject(error);
        });
    });
  }
};

export default ajax;
